﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLSingTowerMall
{
    public class BlockProcess
    {
        private DBSingTowerMallEntities db = new DBSingTowerMallEntities();

        public IQueryable<Level> getLevels()
        {
            var levels = from l in db.Levels
                         select l;
            return levels;
        }

        public Level getLevelByID(int levelid)
        {
            var level = (from lvl in db.Levels
                        where lvl.Level1 == levelid
                             select lvl).FirstOrDefault();
            return level;
        }

        public Block getBlockByID(int id)
        {
            var block = (from blk in db.Blocks
                         where blk.BlockID == id
                         select blk).FirstOrDefault();
            return block;
        }

        public string ModifyImage(int levelid,string mapImageurl)
        {
            try
            {
            var level = (from lvl in db.Levels
                         where lvl.Level1 == levelid
                         select lvl).FirstOrDefault();
            if (level != null)
                level.MapImage = mapImageurl;
                db.SaveChanges();
                return "Sucess";
            }
            catch(Exception e)
            {
                return "Error in modifiying image path";
            }
        }


        public IQueryable<Category> getCategories()
        {
            var categories = from c in db.Categories
                             select c;
            return categories;
        }

        public IQueryable<Block> getBlocks()
        {
            var blocks = from b in db.Blocks
                         select b;
            return blocks;
        }

        public IQueryable<Block> getBlocksByLevel(int lvl)
        {
            var blocks = from b in db.Blocks
                         where b.Level == lvl
                         select b;
            return blocks;
        }

        public string AddBlock(int BlockID, string Type, string BlockLocation, string Dimension)
        {
            try
            {
                Block b = new Block();
                b.BlockID = BlockID;
                b.Dimension = Dimension;
                b.Location = BlockLocation;
                
                if (!b.IsValid)
                    throw new Exception(b.ErrorMessage);


                var blocktype = (from c in db.Categories
                                 where c.CategoryName == Type
                                 select new { c.CategoryName, c.CategoryID }).FirstOrDefault();

                if (string.IsNullOrEmpty(blocktype.CategoryName))
                    throw new Exception("Block Type is Invalid");

                b.Type = blocktype.CategoryID;

                var block = (from bl in db.Blocks
                             where bl.BlockID == BlockID
                             select bl).FirstOrDefault();
                if (block != null)
                    block.BlockID = b.BlockID;

                else
                    db.Blocks.AddObject(b);

                db.SaveChanges();
                return "Block added/modified sucessfully";
            }
            catch(Exception e)
            {
                return e.Message;
            }

        }




        public Category GetCategoryByID(int id)
        {
            var category = (from c in db.Categories
                             where c.CategoryID == id
                             select c).FirstOrDefault();

            return category;
        }
    }
}
